package com.youbug.mall.product_pms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.common.config.valid.SelectGroup;
import com.youbug.common.vo.QueryVo;
import com.youbug.common.vo.R;
import com.youbug.mall.product_pms.entity.PmsCategory;
import com.youbug.mall.product_pms.entity.PmsBrand;
import com.youbug.mall.product_pms.entity.PmsCategoryBrandRelation;
import com.youbug.mall.product_pms.service.ICategoryService;
import com.youbug.mall.product_pms.service.IPmsBrandService;
import com.youbug.mall.product_pms.service.IPmsCategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌 前端控制器
 * </p>
 *
 * @author youbug
 * @since 2023-07-04
 */
@RestController
@RequestMapping("/pmsBrand")
public class PmsBrandController {

    @Autowired
    private IPmsBrandService pmsBrandService;
    @Autowired
    private IPmsCategoryBrandRelationService pmsCategoryBrandRelationService;
    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/getPmsBrands")
    public R<IPage<PmsBrand>> getPmsBrands(@Validated(SelectGroup.class) @RequestBody QueryVo<PmsBrand> queryVo){

        IPage<PmsBrand> userPage = pmsBrandService.getPmsBrands(queryVo);

        return R.ok(userPage);
    }

    @GetMapping("/changeShowStatus/{brandId}/{status}")
    public R<Boolean> changeShowStatus(@PathVariable Long brandId, @PathVariable Integer status){

        Boolean update =  pmsBrandService.changeShowStatus(brandId, status);

        return R.ok(update);
    }

    @GetMapping("/getPmsBrand/{id}")
    public R<PmsBrand> getUser(@PathVariable String id){

        PmsBrand brand = pmsBrandService.getById(id);

        return R.ok(brand);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody PmsBrand brand ){

        boolean save = pmsBrandService.save(brand);

        return  R.ok(save);
    }

    @GetMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable String id){

        boolean remove = pmsBrandService.removeById(id);

        return  R.ok(remove);
    }

    @PostMapping("/update")
    @Transactional(transactionManager = "transactionManagerPms", rollbackFor = Exception.class)
    public R<Boolean> update(@RequestBody PmsBrand brand){

        boolean save = pmsBrandService.updateById(brand);
        pmsCategoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
        return  R.ok(save);
    }

    @PostMapping("/getPmsCategoryBrandRelationPage")
    public R<IPage<PmsCategoryBrandRelation>> getPmsCategoryBrandRelationPage(@RequestBody QueryVo<PmsCategoryBrandRelation> queryVo){

        IPage<PmsCategoryBrandRelation> pmsCategoryBrandRelations = pmsCategoryBrandRelationService.getPmsCategoryBrandRelationPage(queryVo);

        return R.ok(pmsCategoryBrandRelations);
    }


    @GetMapping("/removeCategoryBrandRelation/{id}")
    public R<Boolean> removeCategoryBrandRelation(@PathVariable("id") Long id){

        boolean remove = pmsCategoryBrandRelationService.removeById(id);

        return R.ok(remove);
    }

    @GetMapping("/addCategoryBrandRelation/{brandId}/{categoryId}")
    public R<Boolean> addCategoryBrandRelation(@PathVariable("brandId") Long brandId, @PathVariable("categoryId") Long categoryId){
        PmsCategory category = categoryService.getById(categoryId);
        PmsBrand brand = pmsBrandService.getById(brandId);
        PmsCategoryBrandRelation relation = new PmsCategoryBrandRelation();

        relation.setBrandId(brandId);
        relation.setCatalogId(categoryId);
        relation.setCatalogName(category.getName());
        relation.setBrandName(brand.getName());

        boolean save = pmsCategoryBrandRelationService.save(relation);

        return R.ok(save);
    }

    @GetMapping("/getBrandsByCatalogId/{catId}")
    public R<List<PmsBrand>> getBrandsByCatalogId(@PathVariable(value="catId", required = true) Long catId){

        List<PmsBrand> brands = pmsBrandService.getBrandsByCatId(catId);

        return R.ok(brands);
    }

}

